package com.icode.dao.impl;

import com.icode.core.dto.house.MyHouseDTO;
import com.icode.core.model.house.HouseWeek;
import com.icode.core.model.house.HouseWeekTime;
import com.icode.core.model.house.MyHouse;
import com.icode.core.model.manage.Reserve;
import com.icode.core.model.manage.ReserveTime;
import com.icode.core.shared.JodaTimeUtils;
import com.icode.dao.ReserveDao;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-4-15
 * Time: 上午10:02
 * To change this template use File | Settings | File Templates.
 */
@Repository("reserveDao")
public class ReserveDaoImpl extends EntityDaoImpl implements ReserveDao {


    @Override
    public void changeReserveStatus(String status, String guid) {
        String hql = "update " + Reserve.class.getName() + " a set a.status=:status where a.guid=:guid";
        Query query = currentSession().createQuery(hql);
        query.setParameter("status", status);
        query.setParameter("guid", guid);
        query.executeUpdate();
    }

    @Override
    public List<Reserve> findAllAndOrder() {
        String hql = "from " + Reserve.class.getName() + " a where 0=0 order by a.createTime desc";
        Query query = currentSession().createQuery(hql);
        return query.list();
    }

    @Override
    public List<HouseWeekTime> findHouseWeekTimeBy(String month, HouseWeek houseWeek) {
        String hql = "from " + HouseWeekTime.class.getName() + " a where a.month=:month and a.houseWeek=:houseWeek and a.status=:status";
        Query query = currentSession().createQuery(hql);
        query.setParameter("month", Integer.parseInt(month));
        query.setParameter("houseWeek", houseWeek);
        query.setParameter("status", true);
        return query.list();
    }

    @Override
    public List<Reserve> findReserved() {
        String hql = "from " + Reserve.class.getName() + " a where a.status=:status and a.date like :date";
        Query query = currentSession().createQuery(hql);
        query.setParameter("status", "reserved");
        query.setParameter("date", JodaTimeUtils.getTime("YYYY") + "%");
        return query.list();
    }

    @Override
    public List<Reserve> findAllByMyHouseDTO(MyHouse myHouse) {
        String hql = "from " + Reserve.class.getName() + " a where a.houseInfo=:houseInfo and a.houseWeek =:houseWeek and a.userInfo=:userInfo";
        Query query = currentSession().createQuery(hql);
        query.setParameter("houseInfo", myHouse.houseInfo());
        query.setParameter("houseWeek", myHouse.houseWeek());
        query.setParameter("userInfo", myHouse.userInfo());
        return query.list();
    }

    @Override
    public ReserveTime findReserveTimeByHouseWeekTime(HouseWeekTime houseWeekTime) {
        String hql = "from " + ReserveTime.class.getName() + " a where a.houseWeekTime=:houseWeekTime";
        Query query = currentSession().createQuery(hql);
        query.setParameter("houseWeekTime", houseWeekTime);
        List<ReserveTime> list = query.list();
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public List<Reserve> findReserveByUserInfoUsername(String username) {
        String hql = "from " + Reserve.class.getName() + " a where a.userInfo.username=:username";
        Query query = currentSession().createQuery(hql);
        query.setParameter("username", username);
        return query.list();
    }


}
